package com.ruoyi.labdata.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.labdata.domain.*;
import com.ruoyi.labdata.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.labdata.mapper.LabEmployeeMapper;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 实验室工作人员Service业务层处理
 * 
 * @author winner pan
 * @date 2022-02-02
 */
@Service
public class LabEmployeeServiceImpl implements ILabEmployeeService {
	@Autowired
	private LabEmployeeMapper labEmployeeMapper;
	@Autowired
	private ISysUserService userService;
	@Autowired
    private ILabFileService labFileService;
	@Autowired
	private ILabEmployeeRoleService labEmployeeRoleService;

	@Autowired
	private ILabOrgnizationService labOrgnizationService;

    @Autowired
	private ILabFlowInstanceService labFlowInstanceService;


	private static final String belongType = FileTypeEnum.Employee.getValue();

	/**
	 * 查询实验室工作人员
	 * 
	 * @param employeeId
	 *        L   实验室工作人员ID
	 * @return 实验室工作人员
	 */
	@Override
	public LabEmployee selectLabEmployeeById(Long employeeId) {
		List<LabFile> labFiles = labFileService.getFiles(employeeId, belongType);
		LabEmployee labEmployee = labEmployeeMapper.selectLabEmployeeById(employeeId);
		labEmployee.setImages(labFiles);
		return labEmployee;
	}

	/**
	 * 查询实验室工作人员列表
	 * 
	 * @param labEmployee
	 *            实验室工作人员
	 * @return 实验室工作人员
	 */
	@Override
	public List<LabEmployee> selectLabEmployeeList(LabEmployee labEmployee) {
		return labEmployeeMapper.selectLabEmployeeList(labEmployee);
	}

	/**
	 * 新增实验室工作人员
	 * 
	 * @param labEmployee
	 *            实验室工作人员
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabEmployee(LabEmployee labEmployee) {
		// 查询组织机构信息
		LabOrgnization labOrgnization = labOrgnizationService.selectLabOrgnizationById(labEmployee.getOrgId());
		// 判断流程是否走完
		if (null != labOrgnization.getInstanceId()){
			LabFlowInstance labFlowInstance = new LabFlowInstance();
			labFlowInstance.setInstanceId(labOrgnization.getInstanceId());
			labFlowInstance.setFlowStatusOpt("committed");
			List<LabFlowInstance> labFlowInstances = labFlowInstanceService.selectLabFlowInstanceList(labFlowInstance);
			if (CollUtil.isNotEmpty(labFlowInstances) && labFlowInstances.size() >0){
				throw new RuntimeException("不能进行修改,提交的申报备案的流程未走完!");
			}
		}
		// 更新组织机构中的流程实例id为null
		labOrgnizationService.updateLabOrgnizationInstanceIdNull(labEmployee.getOrgId());
		labEmployee.setCreateBy(SecurityUtils.getUsername());
		labEmployee.setCreateTime(new Date());
		labEmployee.setHireStatusOpt(Long.valueOf(1));
		if(labEmployee.getUserId()==null){
			this.userService.insertUser(labEmployee);
		}else if(labEmployee.getDeptId()!=null && labEmployee.getDeptId().longValue()==labEmployee.getOrgId().longValue()){
			this.userService.updateUser(labEmployee);
		}else{
			this.userService.updateUserProfile(labEmployee);
		}
		int ret = labEmployeeMapper.insertLabEmployee(labEmployee);
		insertUserRole(labEmployee);
		labFileService.insertFiles(labEmployee.getImages(), labEmployee.getEmployeeId(), belongType);
		return ret;
	}

	/**
	 * 修改实验室工作人员
	 * 
	 * @param labEmployee
	 *            实验室工作人员
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabEmployee(LabEmployee labEmployee) {
		// 查询组织机构信息
		LabOrgnization labOrgnization = labOrgnizationService.selectLabOrgnizationById(labEmployee.getOrgId());
		// 判断流程是否走完
		if (null != labOrgnization.getInstanceId()){
			LabFlowInstance labFlowInstance = new LabFlowInstance();
			labFlowInstance.setInstanceId(labOrgnization.getInstanceId());
			labFlowInstance.setFlowStatusOpt("committed");
			List<LabFlowInstance> labFlowInstances = labFlowInstanceService.selectLabFlowInstanceList(labFlowInstance);
			if (CollUtil.isNotEmpty(labFlowInstances) && labFlowInstances.size() >0){
				throw new RuntimeException("不能进行新增,提交的申报备案的流程未走完!");
			}
		}
		// 更新组织机构中的流程实例id为null
		labOrgnizationService.updateLabOrgnizationInstanceIdNull(labEmployee.getOrgId());
		labEmployee.setUpdateBy(SecurityUtils.getUsername());
		labEmployee.setUpdateTime(new Date());
		if(labEmployee.getHireEndDate()!=null){
			labEmployee.setHireStatusOpt(Long.valueOf(0));
		}else{
			labEmployee.setHireStatusOpt(Long.valueOf(1));
		}
		List<LabFile> labFiles = labEmployee.getImages();
	    labFileService.updateFiles(labFiles, labEmployee.getEmployeeId(), belongType);
	    this.userService.updateUserProfile(labEmployee);
	    labEmployeeRoleService.deleteLabEmployeeRoleById(labEmployee.getEmployeeId());
	    insertUserRole(labEmployee);
		return labEmployeeMapper.updateLabEmployee(labEmployee);
	}
	
	/**
	 * 新增用户角色信息
	 * 
	 * @param
	 *
	 */
	public void insertUserRole(LabEmployee labEmployee) {
		Long[] roles = labEmployee.getRoleIds();
		if (StringUtils.isNotNull(roles)) {
			// 新增用户与角色管理
			List<LabEmployeeRole> list = new ArrayList<LabEmployeeRole>();
			for (Long roleId : roles) {
				LabEmployeeRole ur = new LabEmployeeRole();
				ur.setEmployeeId(labEmployee.getEmployeeId());
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				labEmployeeRoleService.batchLabEmployeeRole(list);
			}
		}
	}

	/**
	 * 批量删除实验室工作人员
	 * 
	 * @param employeeIds
	 *            需要删除的实验室工作人员ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabEmployeeByIds(Long[] employeeIds) {
		for(Long employeeId : employeeIds){
    		labFileService.deleteLabFile(employeeId,belongType);
        }
		userService.deleteUserByIds(employeeIds);
		return labEmployeeMapper.deleteLabEmployeeByIds(employeeIds);
	}

	/**
	 * 删除实验室工作人员信息
	 * 
	 * @param employeeId
	 *            实验室工作人员ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabEmployeeById(Long employeeId) {
		labFileService.deleteLabFile(employeeId,belongType);
		userService.deleteUserById(employeeId);
		return labEmployeeMapper.deleteLabEmployeeById(employeeId);
	}

	@Override
	public int updateLabEmployeeLogin(Long employeeId) {
		return labEmployeeMapper.updateLabEmployeeLogin(employeeId);
	}

	@Override
	public List<LabEmployee> chooseUsersQuery(LabEmployee labEmployee) {
		return labEmployeeMapper.chooseUsersQuery(labEmployee);
	}
	
	public List<LabEmployee> chooseEmployeeQuery(LabEmployee labEmployee){
		return labEmployeeMapper.chooseEmployeeQuery(labEmployee);
	}

	public LabEmployee queryEmployeeByName(String employeeNo) {
		return labEmployeeMapper.queryEmployeeByName(employeeNo);
	}

	public Integer checkUserDelete(LabEmployee labEmployee) {
		return this.labEmployeeMapper.checkUserDelete(labEmployee);
	}

	@Override
	public List<LabEmployeeInfoDto> getEmployeeInfo(String employeeNo) {
		return this.labEmployeeMapper.getEmployeeInfo(employeeNo);
	}
	
	public List<LabEmployee> queryEmployeeSCore(Long instanceId){
		return this.labEmployeeMapper.queryEmployeeSCore(instanceId);
	}
}
